package com.zhongxi.guoyuan.view.rewardCoin;

import android.content.Intent;
import android.graphics.Paint;
import android.graphics.drawable.ColorDrawable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AlertDialog;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewTreeObserver;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.RadioButton;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.google.gson.Gson;
import com.umeng.socialize.ShareAction;
import com.umeng.socialize.UMShareAPI;
import com.umeng.socialize.UMShareListener;
import com.umeng.socialize.bean.SHARE_MEDIA;
import com.zhongxi.guoyuan.R;
import com.zhongxi.guoyuan.adapter.AttrAdapter;
import com.zhongxi.guoyuan.adapter.BannerAdapter;
import com.zhongxi.guoyuan.application.MyApplication;
import com.zhongxi.guoyuan.base.BaseActivity;
import com.zhongxi.guoyuan.entity.BaseResponse;
import com.zhongxi.guoyuan.entity.gift.ShareBeautifulResult;
import com.zhongxi.guoyuan.entity.rewardcoin.MallCartResult;
import com.zhongxi.guoyuan.entity.rewardcoin.RewardCoinDetailResult;
import com.zhongxi.guoyuan.fragment.RewardCoin.RewardCoinCommentFragment;
import com.zhongxi.guoyuan.fragment.RewardCoin.RewardCoinDetailFragment;
import com.zhongxi.guoyuan.url.Url;
import com.zhongxi.guoyuan.utils.Constants;
import com.zhongxi.guoyuan.utils.ImageUtils;
import com.zhongxi.guoyuan.utils.MyTextUtils;
import com.zhongxi.guoyuan.view.shopCart.ShoppingCartActivity;
import com.zhongxi.guoyuan.view.user.LoginActivity;
import com.zhongxi.guoyuan.widget.ViewFlow;
import com.zhongxi.guoyuan.widget.sliding.NoScrollListView;
import com.zhongxi.guoyuan.widget.view.MyScrollView;

import org.json.JSONObject;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

public class RewardCoinDetailActivity extends BaseActivity implements MyScrollView.OnScrollListener, View.OnClickListener {

    private ViewFlow myViewFlow;
    private BannerAdapter mBannerAdapter;
    private MyScrollView scrollView;
    private PopupWindow popupWindow;
    private View popupWindow_view;
    private LinearLayout linearBottom;
    private LinearLayout linearLayout;
    private LinearLayout linearScrollBody;
    private LinearLayout linearBegin;
    private LinearLayout linearStop;
    private TextView tvGouwu;
    private TextView tvNature;

    private boolean isMeasured;
    private int mScrollHeight;
    private String goodsid;
    private String type;
    private TextView tvOrign;
    private TextView tvCoin;
    private TextView tvBack;
    private TextView tvRest;
    private TextView tvSell;
    private RewardCoinDetailResult result;
    private TextView tvCollect;
    private TextView tvShare;
    private Button[] tabs;
    private RewardCoinDetailFragment detailFragment;
    private RewardCoinCommentFragment commentFragment;
    private Fragment[] fragments;
    private Button btDetail;
    private Button btComment;
    private int index;
    private int currentIndex;
    private TextView tvName;
    private Map<String, String> map = new HashMap<>();
    private EditText etNum;
    private int flag = 0;
    private AttrAdapter adapter;
    private int tag;
    private RelativeLayout frame;
    private SHARE_MEDIA platform;
    private UMShareAPI mShareAPI;

    @Override
    protected void initView() {
        super.initView();
        setContentView(R.layout.activity_reward_coin_detail);
        initTitle();
        tvTitle.setText("返银超市产品详情");

        mShareAPI = UMShareAPI.get(this);

        goodsid = getIntent().getStringExtra("id");

        tvName = (TextView) findViewById(R.id.tvName);
        tvOrign = (TextView) findViewById(R.id.tvOrign);
        tvCoin = (TextView) findViewById(R.id.tvCoin);
        tvBack = (TextView) findViewById(R.id.tvBack);
        tvRest = (TextView) findViewById(R.id.tvRest);
        tvSell = (TextView) findViewById(R.id.tvSell);
        tvCollect = (TextView) findViewById(R.id.tvCollect);
        tvShare = (TextView) findViewById(R.id.tvShare);

        btDetail = (Button) findViewById(R.id.btDetail);
        btComment = (Button) findViewById(R.id.btComment);

        myViewFlow = (ViewFlow) findViewById(R.id.viewflowproject);
        //上拉悬停
        scrollStop();

        initViewPager();

        //设置高度
        frame = (RelativeLayout) findViewById(R.id.frame);
        ImageUtils.setHeight(this, Constants.detailRate, frame);

    }

    private void initViewPager() {
        tabs = new Button[2];
        tabs[0] = (Button) findViewById(R.id.btDetail);
        tabs[1] = (Button) findViewById(R.id.btComment);
        tabs[0].setSelected(true);
        detailFragment = new RewardCoinDetailFragment();
        commentFragment = new RewardCoinCommentFragment(goodsid);
        fragments= new Fragment[]{detailFragment, commentFragment};
        getSupportFragmentManager().beginTransaction()
                .add(R.id.relaContainer, detailFragment).commit();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.btDetail:
                btDetail.setSelected(true);
                btComment.setSelected(false);
                index = 0;
                break;
            case R.id.btComment:
                btDetail.setSelected(false);
                btComment.setSelected(true);
                index = 1;
                break;
            case R.id.tvGouwu:
            case R.id.tvNature:
                if(null!=MyApplication.getInstance().getUserId()&&!"".equals(MyApplication.getInstance().getUserId())){
                    getPopupWindow();
                }else {
                    showShortToast("用户未登录，请先登录！");
                    Intent intent = new Intent(context, LoginActivity.class);
                    intent.putExtra("position", 1);
                    startActivity(intent);
//                    startAct(LoginActivity.class);
                }
                break;
            case R.id.ivBack:
                finish();
                break;
        }
        if(index != currentIndex){
            FragmentTransaction trx = getSupportFragmentManager().beginTransaction();
            if(fragments[currentIndex].isAdded()){
                trx.hide(fragments[currentIndex]);
            }
            if(!fragments[index].isAdded()){
                trx.add(R.id.relaContainer, fragments[index]);
            }
            trx.show(fragments[index]).commit();
            tabs[index].setSelected(true);
            tabs[currentIndex].setSelected(false);
            currentIndex = index;
        }
    }

    @Override
    protected void initData() {
        super.initData();
        type = "details";
        String str = "&goodsid="+goodsid+"&page="+pageNum;
        getServer(Url.MALL_GOODS_DETAILS, null, str, false, null, "get");
    }

    @Override
    protected void initEvent() {
        super.initEvent();
        tvGouwu.setOnClickListener(this);
        tvNature.setOnClickListener(this);
        ivBack.setOnClickListener(this);
        btDetail.setOnClickListener(this);
        btComment.setOnClickListener(this);
        //收藏
        tvCollect.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //收藏
                getCollection();
            }
        });
        //分享
        tvShare.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //分享
                if (null != popupWindow && popupWindow.isShowing()) {
                    popupWindow.dismiss();
                } else {
                    initPopuWindow();
                }
            }
        });
    }

    /**
     * 收藏
     */
    private void getCollection() {
        if(!MyTextUtils.isFastDoubleClick()){
            map.clear();
            map.put("ftype", "2");
            map.put("refid", goodsid);
            type = "collection";
            getServer(Url.ADD_FAVORITE, map, "", false, null, "post");
        }
    }

    /**
     * 弹出分享界面
     */
    private void initPopuWindow() {
        popupWindow_view = getLayoutInflater().inflate(R.layout.pop_shared, null, false);
        popupWindow = new PopupWindow(popupWindow_view, LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT, true);
        popupWindow.setBackgroundDrawable(new ColorDrawable(0xb0000000));
        popupWindow.setFocusable(true);
        popupWindow.showAtLocation(tvShare, Gravity.BOTTOM, 0, 0);
        TextView tv_cancle = (TextView) popupWindow_view.findViewById(R.id.tv_cancle);
        LinearLayout ll_qq = (LinearLayout) popupWindow_view.findViewById(R.id.ll_qq);
        LinearLayout ll_wechat = (LinearLayout) popupWindow_view.findViewById(R.id.ll_wechat);
        LinearLayout ll_qzone = (LinearLayout) popupWindow_view.findViewById(R.id.ll_qzone);
        LinearLayout ll_friendscircle = (LinearLayout) popupWindow_view.findViewById(R.id.ll_friendscircle);
        View.OnClickListener shareClickListener = new View.OnClickListener(){

            @Override
            public void onClick(View v) {
                switch (v.getId()){
                    case R.id.ll_qq:
                        platform = SHARE_MEDIA.QQ;
                        break;
                    case R.id.ll_wechat:
                        platform = SHARE_MEDIA.WEIXIN;
                        break;
                    case R.id.ll_qzone:
                        platform = SHARE_MEDIA.QZONE;
                        break;
                    case R.id.ll_friendscircle:
                        platform = SHARE_MEDIA.WEIXIN_CIRCLE;
                        break;
                }
                new ShareAction(RewardCoinDetailActivity.this).setPlatform(platform).setCallback(umShareListener).withTitle("购物何必再花钱！消费多少返多少=免费购物！")
                        .withText("手机里安装一个自己的APP，天天“摇钱树+搜红包”,千百商品消费多少返多少银币=免费购物！").withTargetUrl(Url.BASE_URL+"/share/welfaremall/goodsdetails?gid="+goodsid)
                        .share();
                popupWindow.dismiss();
            }
        };
        ll_qq.setOnClickListener(shareClickListener);
        ll_wechat.setOnClickListener(shareClickListener);
        ll_qzone.setOnClickListener(shareClickListener);
        ll_friendscircle.setOnClickListener(shareClickListener);
        tv_cancle.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                popupWindow.dismiss();
            }
        });
    }

    private UMShareListener umShareListener = new UMShareListener() {

        @Override
        public void onResult(SHARE_MEDIA platform) {
            if (platform.name().equals("WEIXIN_FAVORITE")) {
                showShortToast("收藏成功啦");
            } else {
                showShortToast("分享成功啦");
            }
            type = "shareget";
            getServer(Url.SHARE, null, "", false, null, "get");
        }

        @Override
        public void onError(SHARE_MEDIA platform, Throwable t) {
            showShortToast("分享失败啦");
        }

        @Override
        public void onCancel(SHARE_MEDIA platform) {
            showShortToast("分享取消了");
        }
    };

    @Override
    protected void processSuccessResult(String response) {
        super.processSuccessResult(response);
        Gson gson = new Gson();
        try{
            if("details".equals(type)){
                result = gson.fromJson(response, RewardCoinDetailResult.class);
                MyApplication.getInstance().setSpValue(result.getSafeobj());
                List<String> data = result.getGoodsInfo().getImages();
                if(data.size()>0) {
                    myViewFlow.setSideBuffer(data.size()); // 实际图片数量
                    if(mBannerAdapter == null){
                        mBannerAdapter = new BannerAdapter(context, data);
                        myViewFlow.setAdapter(mBannerAdapter);
                    }else{
                        mBannerAdapter.replaceAll(data);
                    }
                }
                setDetails();
            }else if("addcart".equals(type)){
                MallCartResult cartResult = gson.fromJson(response, MallCartResult.class);
                MyApplication.getInstance().setSpValue(cartResult.getSafeobj());
                if(1==tag){
                    Intent intent = new Intent(context, ShoppingCartActivity.class);
                    intent.putExtra("flag", flag);
                    startActivity(intent);
                }else{
                    showShortToast("加入购物车成功！");
                }
            }else if("collection".equals(type)){
                BaseResponse result = gson.fromJson(response, BaseResponse.class);
                MyApplication.getInstance().setSpValue(result.getSafeobj());
                showShortToast("收藏成功！");
            }else if("shareget".equals(type)){
                ShareBeautifulResult result = gson.fromJson(response, ShareBeautifulResult.class);
                MyApplication.getInstance().setSpValue(result.getSafeobj());
                showPopupWindow(result.getNum());
            }
        }catch (Exception e) {
            e.printStackTrace();
        }
    }
    private void showPopupWindow(String num) {
        LayoutInflater inflater = LayoutInflater.from(this);
        LinearLayout layout = (LinearLayout) inflater.inflate(R.layout.dialog_share, null);
        final AlertDialog dialog = new AlertDialog.Builder(context).create();
        dialog.show();
        dialog.getWindow().setContentView(layout);

        ImageView ivCancel = (ImageView) layout.findViewById(R.id.ivCancel);
        TextView tvTips = (TextView) layout.findViewById(R.id.tvTips);
        TextView tvQuit = (TextView) layout.findViewById(R.id.tvQuit);

        ivCancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
            }
        });
        tvTips.setText("成功分享获得"+num+"个美币");
        tvQuit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
                finish();
            }
        });
    }
    @Override
    protected void ProcessFailResult(String response) {
        super.ProcessFailResult(response);
        Gson gson = new Gson();
        try{
            BaseResponse result = gson.fromJson(response, BaseResponse.class);
            if(null!=result.getSafeobj()&&!"".equals(result.getSafeobj())){
                MyApplication.getInstance().setSpValue(result.getSafeobj());
            }
            if(result.getCode()==2101){
                showShortToast("商品数据完善中，敬请期待");
                finish();
            }else if(result.getCode()==2102){
                showShortToast("购物车商品超限");
            }else if(result.getCode()==2103){
                showShortToast("该商品已下架");
            }else if(result.getCode()==2104){
                showShortToast("该商品库存不足");
            }else if(result.getCode()==2105){
                showShortToast("商品信息不完善，敬请期待");
            }else if(result.getCode()==2106){
                showShortToast("当前商品数量超限");
            }else if(result.getCode()==6206){
                showShortToast("您已收藏过该项目");
            }else if(result.getCode()==1000){
                showShortToast("商品已下架");
                finish();
            }
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    /**
     * 商品详细
     */
    private void setDetails() {
        try{
            if(null!=result.getGoodsInfo()&&!"".equals(result.getGoodsInfo())){
                RewardCoinDetailResult.RewardCoinDetailData goodsInfo = result.getGoodsInfo();
                if(null!=goodsInfo.getName()&&!"".equals(goodsInfo.getName())){
                    tvName.setText(goodsInfo.getName());
                }
                if(null!=goodsInfo.getPrice()&&!"".equals(goodsInfo.getPrice())){
                    tvCoin.setText(goodsInfo.getPrice());
                    tvBack.setText("元[返"+new BigDecimal(goodsInfo.getPrice()).intValue()+"银币]");
                }
                if(null!=goodsInfo.getMarketprice()&&!"".equals(goodsInfo.getMarketprice())){
                    tvOrign.setText("原价："+goodsInfo.getMarketprice());
                    tvOrign.setPaintFlags(Paint.STRIKE_THRU_TEXT_FLAG);
                }
            }
            if(null!=result.getGoodsAttr()&&!"".equals(result.getGoodsAttr())){
                RewardCoinDetailResult.RewardCoinInfo goodsAttr = result.getGoodsAttr();
                if(null!=goodsAttr.getInventory()&&!"".equals(goodsAttr.getInventory())){
                    tvRest.setText("库存："+goodsAttr.getInventory());
                }
                if(null!=goodsAttr.getSoldnum()&&!"".equals(goodsAttr.getSoldnum())){
                    tvSell.setText("已销售："+goodsAttr.getSoldnum());
                }
            }
            //初始化详情数据
            detailFragment.initProjectData(result);
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    private void scrollStop() {
        scrollView = (MyScrollView) findViewById(R.id.scrollView);
        linearLayout = (LinearLayout) findViewById(R.id.linearLayout);
        linearScrollBody = (LinearLayout) findViewById(R.id.linearScrollBody);
        linearBegin = (LinearLayout) findViewById(R.id.linearBegin);
        linearStop = (LinearLayout) findViewById(R.id.linearStop);
        linearBottom = (LinearLayout) findViewById(R.id.linearBottom);
        tvGouwu = (TextView) findViewById(R.id.tvGouwu);
        tvNature = (TextView) findViewById(R.id.tvNature);
        scrollView.setOnScrollListener(this);

        ViewTreeObserver observer = linearLayout.getViewTreeObserver();
        observer.addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
            @Override
            public boolean onPreDraw() {
                if (!isMeasured) {
                    mScrollHeight = linearLayout.getMeasuredHeight();
                    isMeasured = true;
                }
                return true;
            }
        });
        scrollView.setOnScrollListener(this);
    }

    @Override
    public void onScroll(int scrollY) {
        if(scrollY >= mScrollHeight){
            if(linearScrollBody.getParent() != linearStop){
                linearBegin.removeView(linearScrollBody);
                linearStop.addView(linearScrollBody);
            }
        }else{
            if(linearScrollBody.getParent() != linearBegin){
                linearStop.removeView(linearScrollBody);
                linearBegin.addView(linearScrollBody);
            }
        }
    }

    @Override
    public void scrollBottom() {

    }

    private void getPopupWindow() {
        if (null != popupWindow && popupWindow.isShowing()) {
            popupWindow.dismiss();
            return;
        } else {
            initPopupWindow();
        }
    }

    private void initPopupWindow() {
        popupWindow_view = getLayoutInflater().inflate(R.layout.base_cart_info, null);
        popupWindow = new PopupWindow(popupWindow_view, LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
        popupWindow.setBackgroundDrawable(new ColorDrawable(0));

        ImageView ivImg = (ImageView) popupWindow_view.findViewById(R.id.ivImg);
        ImageView ivCancel = (ImageView) popupWindow_view.findViewById(R.id.ivCancel);
        TextView tvName = (TextView) popupWindow_view.findViewById(R.id.tvName);
        TextView tvOrign = (TextView) popupWindow_view.findViewById(R.id.tvOrign);
        TextView tvPrice = (TextView) popupWindow_view.findViewById(R.id.tvPrice);
        TextView tvRest = (TextView) popupWindow_view.findViewById(R.id.tvRest);
        TextView tvAddSure = (TextView) popupWindow_view.findViewById(R.id.tvAddSure);
        TextView tvAdd = (TextView) popupWindow_view.findViewById(R.id.tvAdd);
        TextView tvSub = (TextView) popupWindow_view.findViewById(R.id.tvSub);
        TextView tvSure = (TextView) popupWindow_view.findViewById(R.id.tvSure);
        RadioButton rbSend = (RadioButton) popupWindow_view.findViewById(R.id.rbSend);
        RadioButton rbSelf = (RadioButton) popupWindow_view.findViewById(R.id.rbSelf);
        etNum = (EditText) popupWindow_view.findViewById(R.id.etNum);

        NoScrollListView lvList = (NoScrollListView) popupWindow_view.findViewById(R.id.lvList);

        //消失
        ivCancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(popupWindow!=null&&popupWindow.isShowing()){
                    popupWindow.dismiss();
                    popupWindow = null;
                }
            }
        });

        popupWindow_view.measure(View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED);
        int[] location = new int[2];
        linearBottom.getLocationOnScreen(location);
        popupWindow.showAtLocation(linearBottom, Gravity.BOTTOM, location[0], location[1]-popupWindow.getHeight());

        try {
            if(null !=result.getGoodsInfo()&&!"".equals(result.getGoodsInfo())){
                if (null != result.getGoodsInfo().getImages() && !"".equals(result.getGoodsInfo().getImages())&&result.getGoodsInfo().getImages().size()>0) {
                    Glide.with(context).load(Url.IMG_URL + result.getGoodsInfo().getImages().get(0)).placeholder(R.mipmap.iv_eg).into(ivImg);
                }
                if(null != result.getGoodsInfo().getName()&& !"".equals(result.getGoodsInfo().getName())){
                    tvName.setText(result.getGoodsInfo().getName());
                }
                if(null != result.getGoodsInfo().getPrice()&&!"".equals(result.getGoodsInfo())){
                    tvPrice.setText(result.getGoodsInfo().getPrice()+"元[返"+new BigDecimal(result.getGoodsInfo().getPrice()).intValue()+"银币]");
                }
                if(null!=result.getGoodsInfo().getMarketprice()&&!"".equals(result.getGoodsInfo().getMarketprice())){
                    tvOrign.setText("原价："+result.getGoodsInfo().getMarketprice());
                    tvOrign.setPaintFlags(Paint.STRIKE_THRU_TEXT_FLAG);
                }
            }
            if(null!=result.getGoodsAttr()&&!"".equals(result.getGoodsAttr())){
                if(null!=result.getGoodsAttr().getInventory()&&!"".equals(result.getGoodsAttr().getInventory())){
                    tvRest.setText("库存："+result.getGoodsAttr().getInventory());
                }
                if(null!=result.getGoodsAttr().getAttr()&&!"".equals(result.getGoodsAttr().getAttr())){
                    adapter = new AttrAdapter(context, result.getGoodsAttr().getAttr());
                    lvList.setAdapter(adapter);
                }
            }
            //添加到购物车
            tvAddSure.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    tag = 0;
                    if(adapter.getList().size()<result.getGoodsAttr().getAttr().size()){
                        showShortToast("请先选择属性");
                    }else if(flag == 0){
                        showShortToast("请选择配送方式");
                    }else{
                        popupWindow.dismiss();
                        addCart();
                    }
                }
            });
            //+
            tvAdd.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    String number = etNum.getText().toString().trim();
                    if(!"100".equals(number)){
                        number = Integer.parseInt(number)+1+"";
                        etNum.setText(number);
                    }else{
                        showShortToast("购买数量不能大于100");
                    }
                }
            });
            //-
            tvSub.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    String number = etNum.getText().toString().trim();
                    if(!"1".equals(number)){
                        number = Integer.parseInt(number)-1+"";
                    }
                    etNum.setText(number);
                }
            });
            //支付方式
            rbSend.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    flag = 2;
                }
            });
            rbSelf.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    flag = 1;
                }
            });
            //立即支付
            tvSure.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    tag = 1;
                    if(adapter.getList().size()<result.getGoodsAttr().getAttr().size()){
                        showShortToast("请先选择属性");
                    }else if(flag == 0){
                        showShortToast("请选择配送方式");
                    }else{
                        popupWindow.dismiss();
                        addCart();
                    }
                }
            });
        }catch (Exception e){
            e.printStackTrace();
        }

    }

    /**
     * 添加到购物车
     */
    private void addCart() {
        type = "addcart";
        map.clear();
        map.put("gid", result.getGoodsInfo().getId());
        map.put("goodsNum", etNum.getText().toString().trim());
        map.put("getM", flag+"");
        map.put("attrs", getJsonData());
        getServer(Url.MALL_ADDTO_CART, map, "", false, null, "post");
    }

    /**
     * 封装JSON字符串
     * @return
     */
    private String getJsonData() {
        JSONObject obj = new JSONObject();
        try{
            List<Map<String, String>> list = adapter.getList();
            for(int i=0; i<list.size(); i++){
                Map<String, String> map = list.get(i);
                Set keys = map.keySet();
                if (keys != null) {
                    Iterator iterator = keys.iterator();
                    while (iterator.hasNext()) {
                        Object key = iterator.next();
                        String value = map.get(key);
                        obj.put(key.toString(), value);
                    }
                }
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return obj.toString();
    }
}